<template>
    <div>
        <van-nav-bar left-arrow @click-left="onClickLeft()" title="用户注册"></van-nav-bar>
        <div class="wrap">
            <div class="form-content">
                <div class="form-title">
                    <div>Register For </div>
                    <div style="margin-left:10px;"><van-image src="https://create.nightowl.top/static/images/logo.png"
                            height="40" width="40"></van-image></div>
                    <div>易搭衣橱</div>
                </div>
                <van-form @submit="onSubmit">
                    <van-cell-group inset>
                        <van-field icon-prefix="iconfont" clearable="true" left-icon="user" v-model="username"
                            label-width="60px" name="username" label="用户名" placeholder="请输入用户名~"
                            label-class="field-username" :rules="[{ required: true, message: ' (°ー°〃)' }]">
                            <template #left-icon>
                                <van-icon class-prefix="iconfont" name="user" color="#f3a8df" size="20" /></template>
                        </van-field>
                        <van-field v-model="password" icon-prefix="iconfont" clearable="true" left-icon="password"
                            label-width="60px" type="password" name="password" label="密码" placeholder="请输入密码~"
                            :rules="[{ required: true, message: ' (°ー°〃)' }]" label-class="field-password">
                            <template #left-icon>
                                <van-icon class-prefix="iconfont" name="password" color="#37e962" size="20" /></template>
                        </van-field>
                        <van-field v-model="passwordRepeat" icon-prefix="iconfont" clearable="true" left-icon="password"
                            label-width="60px" type="passwordRepeat" name="passwordRepeat" label="重复密码"
                            placeholder="请再次输入密码~" :rules="[{ required: true, message: ' (°ー°〃)' }]"
                            label-class="field-password">
                            <template #left-icon>
                                <van-icon class-prefix="iconfont" name="password" color="#37e962" size="20" /></template>
                        </van-field>
                        <div class="check-box">
                            <van-checkbox v-model="isAgreeRules" icon-size="20px">同意<span style="color:#3792ed"
                                    @click="navToPage('/clause')">易搭衣橱用户协议</span>~</van-checkbox>
                        </div>
                    </van-cell-group>
                    <div style="margin: 16px;">
                        <van-button round block type="primary" native-type="submit">注册</van-button>
                    </div>
                    <!-- <div class="text-link">
                        <div @click="navToPage('/login')">&lt;&ensp;返回登录账号</div>
                    </div> -->
                </van-form>
            </div>
        </div>
    </div>
</template>
  
<script setup>
import { ref } from "vue";
import { useRouter } from 'vue-router';
import _service from "../request/axios";
import { showToast } from "vant";
let username = ref('');
let password = ref('');
let passwordRepeat = ref('');
const router = useRouter();
let isAgreeRules = ref(false);
function onClickLeft() {
    router.back();
}
function navToPage(route) {
    router.push({
        path: route
    });
}
const onSubmit = (values) => {
    if (username.value.length < 6 || password.value.length < 6) {
        showToast("用户名或密码长度过短~");
        return;
    }
    if (password.value != passwordRepeat.value) {
        showToast("两次密码不一致~");
        return;
    }
    if (!isAgreeRules.value) {
        showToast("请同意用户协议~");
        return;
    }
    _service.post("/user/register", values).then(res => {
        if (res) {
            if (!res.code) {
                showToast("注册成功~");
                //写入用户数据缓存
                router.back();
            } else {
                showToast(res.msg);
            }
        } else {
            showToast("服务端错误，请稍后再试~")
        }
    })
    console.log('submit', values);
};
</script>
  
<style lang="less">
.form-content {
    padding: 20px;
    box-sizing: border-box;

    .form-title {
        width: 100%;
        text-align: center;
        font-size: 40rpx;
        font-weight: bold;
        margin-bottom: 200rpx;
        color: #87abf2;
        margin-bottom: 30px;
    }

    .field-username {
        color: #f3a8df
    }

    .field-password {
        color: #37e962
    }

    .text-link {
        display: flex;
        color: #b9bdbe;
        padding: 0px 20px;
        box-sizing: border-box;
        justify-content: space-between;
        font-size: 14px;
    }

    .check-box {
        padding: 10px 15px;
    }
}

.buttom-box {
    padding: 0px 35px;

    .login-type {
        display: flex;
        justify-content: space-between;
    }
}

.van-cell__value {
    justify-content: flex-start;
}

.van-field__control {
    max-width: 100px;
}

.van-field {
    align-items: center;
}
</style>
  